package com.liuya.safe.entitle.handler;

import com.liuya.base.BaseModel;
import com.liuya.common.WebUtil;
import com.liuya.safe.model.SafePrivilege;
import com.liuya.safe.entitle.service.PrivilegeManagerService;
import com.liuya.safe.tree.AbstractTreeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 策略授权树处理类
 */
@Component("privilegeTreeHandler")
public class PrivilegeTreeHandler extends AbstractTreeHandler implements Serializable {

    @Autowired
    protected PrivilegeManagerService privilegeManagerService;

    @Override
    public String getPageTitle() {
        return "Privilege_Tree";
    }

    @Override
    public String getCreatePage() {
        return "privilege/create";
    }

    @Override
    public String getNodeName(BaseModel node) {
        SafePrivilege pvlg = (SafePrivilege) node;
        return pvlg.getName();
    }

    @Override
    public Collection getAllNodes() {
        Collection pvlgs = privilegeManagerService.getAllBusinessPrivileges();
        List allPvlgs = new ArrayList(pvlgs.size() + 1);
        allPvlgs.addAll(pvlgs);
        return allPvlgs;
    }

    @Override
    public BaseModel getNode(HttpServletRequest req) {
        SafePrivilege pvlg = new SafePrivilege();
        String id = getId(req);
        String pid = getParentId(req);
        String name = req.getParameter("name");
        String desc = req.getParameter("description");
        boolean isLeaf = WebUtil.getBooleanParameter(req, "isLeaf", false);
        String constantName = req.getParameter("constantName");
        String target = req.getParameter("target");
        String url = req.getParameter("url");
        boolean display = WebUtil.getBooleanParameter(req, "display", true);
        int orderNum = WebUtil.getIntParameter(req, "orderNum", 0);
        int status = WebUtil.getIntParameter(req, "status", 1);

        pvlg.setConstantName(constantName);
        pvlg.setTarget(target);
        pvlg.setUrl(url);
        pvlg.setId(id);
        pvlg.setIsLeaf(isLeaf);
        pvlg.setDisplay(display);
        pvlg.setName(name);
        pvlg.setPid(pid);
        pvlg.setDescription(desc);
        pvlg.setOrderNum(orderNum);
        pvlg.setStatus(status);
        pvlg.setType(0);
        return pvlg;
    }

    @Override
    public BaseModel getNodeByName(String name) {
        Collection nodes = privilegeManagerService.getAllBusinessPrivileges();
        for (Iterator iter = nodes.iterator(); iter.hasNext();) {
            SafePrivilege node = (SafePrivilege) iter.next();
            if (node.getName().equals(name)) {
                return node;
            }
        }

        nodes = privilegeManagerService.getAllNonRolePrivileges();
        for (Iterator iter = nodes.iterator(); iter.hasNext();) {
            SafePrivilege node = (SafePrivilege) iter.next();
            if (node.getName().equals(name)) {
                return node;
            }
        }
        return null;
    }

    @Override
    public boolean addNode(Object node) {
        SafePrivilege pvlg = (SafePrivilege) node;
        try{
            privilegeManagerService.addPrivilege(pvlg);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean deleteNode(String id) {
        privilegeManagerService.deletePrivilegeCascade(id);
        return true;
    }

    @Override
    public boolean updateNode(Object node) {
        SafePrivilege db = (SafePrivilege) node;
        try{
            privilegeManagerService.updatePrivilege(db);
        } catch(Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public boolean moveNode(String targetId, String nodeId, int position) {
        SafePrivilege node = privilegeManagerService.getPrivilege(nodeId);
        SafePrivilege targetNode = privilegeManagerService.getPrivilege(targetId);
        privilegeManagerService.movePrivilege(node, targetNode, position);
        return true;
    }

    @Override
    public String getContextMenuPage() {
        return "privilege/contextMenu";
    }

    @Override
    public BaseModel getNodeById(String id) {
        return privilegeManagerService.getPrivilege(id);
    }

    @Override
    public String getModifyPage() {
        return "privilege/modify";
    }

    @Override
    public String getTreeType() {
        return "privilege";
    }

    @Override
    public boolean copyNode(Object node) {
        //privilege doesn't support copy
        return false;
    }
}
